package com.sphd.education.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.JsonObject;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.adapter.HistoryAdapter;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.info.MyInfo;
import com.sphd.education.modle.EducationUrl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 历史课程
 */

public class LessonsHistoryActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.history_listview)
    ListView historyListview;
    HistoryAdapter historyAdapter;
    List<LessonInfo> lessonInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lessons_history);
        ButterKnife.bind(this);
        toolbar.setTitle("历史课程");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initView();
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
//        lessonInfoList = (List<LessonInfo>) intent.getSerializableExtra("lessonInfoList");
        lessonInfoList = new ArrayList<>();
        addDate();
        historyAdapter = new HistoryAdapter(this, lessonInfoList);
        historyListview.setAdapter(historyAdapter);
    }

    private void addDate() {
        MyInfo myInfo = DataSupport.findLast(MyInfo.class);
        String url= EducationUrl.HttpTitle+EducationUrl.MyHistoryLesson+"studentId="+myInfo.getUserid();
//        String url = EducationUrl.HttpTitle + EducationUrl.MyHistoryLesson + "studentId=1";
        Log.e("url", url);

        JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.e("response", response.toString());
                getList(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        EducationApplication.getRequestQueue().add(jsonObjectRequest);
    }

    private void getList(JSONArray response) {
        for (int i = 0; i < response.length(); i++) {
            try {
                JSONObject jsonObject = response.getJSONObject(i);
                LessonInfo lessonInfo = new LessonInfo();
                lessonInfo.setSchoolName(jsonObject.getString("orgName"));
                lessonInfo.setName(jsonObject.getString("lessonName"));
                lessonInfo.setBanji(jsonObject.getString("name"));
                lessonInfo.setBeginTime(jsonObject.getString("startDate1"));
                lessonInfo.setEndTime(jsonObject.getString("endDate1"));
                if (!jsonObject.getString("operatetype").equals("0")){
                    if (jsonObject.getString("operatetype").equals("1")){
                        lessonInfo.setMyEvalution("差评");
                    }else if (jsonObject.getString("operatetype").equals("2")){
                        lessonInfo.setMyEvalution("中评");
                    }else if (jsonObject.getString("operatetype").equals("3")){
                        lessonInfo.setMyEvalution("好评");
                    }
                    lessonInfo.setMyBack(jsonObject.getString("comment"));
                    lessonInfo.setAnonymousType(jsonObject.getString("anonymousType"));
                }else {
                    lessonInfo.setMyEvalution("");
                    lessonInfo.setMyBack("");
                    lessonInfo.setAnonymousType("");
                }

                lessonInfoList.add(lessonInfo);
                historyListview.setAdapter(historyAdapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void findViewById() {

    }

    @Override
    public void myNet() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_lesson_my_history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == android.R.id.home) {

            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
